module("HomeScene", package.seeall)

require("src.ui.MainMenuUI")
require("src.Home.PlayerUI")
require("src.Battle.BattleScene")
require("src.BattleMap.MapScene")
require("src.Home.AnimMenu")

function create()
	local scene = cc.Scene:create()

	init(scene)

	return scene
end

function init(scene)
    local readnode = ccs.SceneReader:getInstance():createNodeWithSceneFile("res/HomeScene.json")

    local h1Node = readnode:getChildByTag(1)
    h1Node:setContentSize(h1Node:getComponent("h1"):getNode():getContentSize())
    local h2Node = readnode:getChildByTag(2)
    h2Node:setContentSize(h2Node:getComponent("h2"):getNode():getContentSize())
    local h3Node = readnode:getChildByTag(3)
    h3Node:setContentSize(h3Node:getComponent("h3"):getNode():getContentSize())

    h1Node:removeFromParent()
    h2Node:removeFromParent()
    h3Node:removeFromParent()

    local mainNode = cc.ParallaxNode:create()
    mainNode:addChild(h3Node, -1, cc.p(0.25,0.5), cc.p(h3Node:getContentSize().width/2,h3Node:getContentSize().height/2))
    mainNode:addChild(h2Node, 0, cc.p(0.5,1.0), cc.p(h2Node:getContentSize().width/2,h2Node:getContentSize().height/2) )
    mainNode:addChild(h1Node, 1, cc.p(1,2), cc.p(h1Node:getContentSize().width/2,h1Node:getContentSize().height/2))
    h1Node:setAnchorPoint(cc.p(0,0))
    h2Node:setAnchorPoint(cc.p(0,0))
    h3Node:setAnchorPoint(cc.p(0,0))
	
    local scrollview = cc.ScrollView:create()
    local screenSize = cc.Director:getInstance():getWinSize()
    scrollview:setViewSize(cc.size(screenSize.width,screenSize.height))

	mainNode:setPosition(cc.p(0, 0))
	mainNode:setContentSize(h1Node:getContentSize())

    scrollview:setContainer(mainNode)
	scrollview:setBounceable(false)
	scrollview:updateInset()

	scrollview:ignoreAnchorPointForPosition(true)
	scrollview:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL)
	scrollview:setClippingToBounds(true)
	scrollview:setContentOffset(cc.p((screenSize.width - h1Node:getContentSize().width)/2, 0), false)

	AnimMenu.create(h1Node:getChildByTag(1):getComponent("CCSprite"):getNode(), 1)
	AnimMenu.create(h1Node:getChildByTag(2):getComponent("CCSprite"):getNode(), 1)
	AnimMenu.create(h2Node:getChildByTag(1):getComponent("CCSprite"):getNode(), 2)
	AnimMenu.create(h2Node:getChildByTag(2):getComponent("CCSprite"):getNode(), 2)
	
	--加载UI层
	local uiLayer = cc.Layer:create()
	local uiMain = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MainUI/MainUI.ExportJson")
	local uiPlayerHeader = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MainUI/PlayerHeader.ExportJson")
	local uiStatusBar = ccs.GUIReader:getInstance():widgetFromJsonFile("res/UI/MainUI/StatusBar.ExportJson")
	local uiMainMenu = MainMenuUI.create()
	uiMain:setSize(cc.size(screenSize.width, screenSize.height))

	uiPlayerHeader:setPosition(0, screenSize.height - uiPlayerHeader:getContentSize().height)
	uiStatusBar:setPosition(screenSize.width - uiStatusBar:getContentSize().width, screenSize.height - uiStatusBar:getContentSize().height)
	uiMainMenu:setPosition(screenSize.width - uiMainMenu:getContentSize().width, 0)

	local imageIcon = uiPlayerHeader:getChildByName("ImageView_Icon")
	imageIcon:addTouchEventListener(
		function ( sender, eventType )
			if (eventType == ccui.TouchEventType.ended) then
				local layer = PlayerUI.create()
				layer:setSize(screenSize)
				scene:addChild(layer, 2)
			end
		end
	)

	uiMain:addChild(uiStatusBar)
	uiMain:addChild(uiPlayerHeader)
	uiMain:addChild(uiMainMenu)
	
	uiLayer:addChild(uiMain)
	uiLayer:setLocalZOrder(1)

	scene:addChild(scrollview)
	scene:addChild(uiLayer)
end

